package net.oschina.app.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseNewsBLogAdapter;
import net.oschina.app.adapter.BaseNewsInfosAdapter;
import net.oschina.app.interf.ResultState;
import net.oschina.app.ui.view.ContentPager;
import net.oschina.app.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by wei03 on 2017/1/13.
 */

public abstract class BaseFragment<T> extends Fragment implements ResultState {
    protected int mPageIndex = 0;
    protected ContentPager mContentPager;
    protected PtrClassicFrameLayout ptrClassicFrameLayout;
    private RecyclerView mRecycleView;
    private BaseAdapter mRvAdapter;
    protected ArrayList<T> mList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentPager = new ContentPager(getContext()) {
            @Override
            public View onCreateSuccessView() {
                ptrClassicFrameLayout = (PtrClassicFrameLayout) CommonUtils.inflate(R.layout.fragment_base);

                mRecycleView = (RecyclerView) ptrClassicFrameLayout.findViewById(R.id.recycleView);
                //添加华丽的recycleView分割线
                mRecycleView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
                mRvAdapter = getRvAdapter();
                mRvAdapter.setDatas(mList);
                mRecycleView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
                RecyclerAdapterWithHF adapterWithHF = new RecyclerAdapterWithHF(mRvAdapter);
                mRecycleView.setAdapter(adapterWithHF);
                ptrClassicFrameLayout.setLoadMoreEnable(true);
                if (adapterWithHF != null) {
                    adapterWithHF.setOnItemClickListener(new RecyclerAdapterWithHF.OnItemClickListener() {
                        @Override
                        public void onItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
//                           OnSubItemClick(view, data);
                            OnSubItemClick(adapter, vh, position);
                        }
                    });
                }
                return ptrClassicFrameLayout;
            }

            @Override
            public int onLoadData() {
                return onLoadSubData();
            }
        };
        return mContentPager;
    }

    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        if (vh instanceof BaseNewsInfosAdapter.NewsInfosHolder) {
            BaseNewsInfosAdapter.NewsInfosHolder newsInfosHolder = (BaseNewsInfosAdapter.NewsInfosHolder) vh;
            newsInfosHolder.mTvTitleSynthesize.setTextColor(Color.GRAY);
            Object tag = newsInfosHolder.itemView.getTag();
            saveToLocal(tag.toString(),(int) tag);
        }
        if (vh instanceof BaseNewsBLogAdapter.NewsBlogHolder) {
            BaseNewsBLogAdapter.NewsBlogHolder newsBlogHolder = (BaseNewsBLogAdapter.NewsBlogHolder) vh;
            newsBlogHolder.mTvTitleSynthesize.setTextColor(Color.GRAY);
            Object tag = newsBlogHolder.itemView.getTag();
            saveToLocal(tag.toString(), ((int) tag));
        }
    }

    private void saveToLocal(String key, int id) {
        SharedPreferences sp = CommonUtils.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        sp.edit().putInt(key, id).commit();
    }

    //空实现,子类可不重写...
    protected String getFragmentTitle() {
        return "开源中国";
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPageIndex = 0;
                mList.clear();
                mContentPager.loadData();
                ptrClassicFrameLayout.refreshComplete();
            }
        });
        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                mContentPager.loadData();
                ptrClassicFrameLayout.loadMoreComplete(true);
            }
        });

/*        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);*/
    }

    protected void OnSubItemClick(View view, Object data) {
    }


    protected abstract BaseAdapter getRvAdapter();

    protected abstract int onLoadSubData();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContentPager.loadData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
